/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

/**
 *
 * @author Bhushan
 */
import java.util.Random;
import java.awt.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.urls.*;
import org.jfree.data.category.*;
import org.jfree.data.general.*;
import java.io.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;
import java.util.Date;

public class Game {

    public static double step = .02; // Step size to increase or decrease effort.
    
    public static double compute(int userID, int option) {
        double effort = -1;
        boolean flag = false;
        try {

            Computation compute1 = new Computation();
            effort = DataAccess.getLatestEffortforUser(userID);
            if (option == 1) {
                effort = effort + step;   
                flag = true;
            }
            if (option == -1) {
                if (effort <= 0) {
                    effort = 0;
                } else {
                    effort = effort - step;
                }
                flag = true;
            }

            compute1.compute_utility(userID, effort, flag);


            //  Game.displayMultipleUtils(userID);
            // graphUtility(userID);
            
            Game.displayBarChart(userID, compute1.getHint(userID,step));
            if (flag) {
                compute1.createEffortChart();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return effort;
    }

    // not required for this version
     /*
     * udates player utility as a line graph
     */
    public static void updateUtility(int userID) {
        try {

            //  graphUtility(userID);
            displayMultipleUtils(userID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * Displays the solution
     */

    public static void showRecommendation(int userID) {
        try {
            Computation compute1 = new Computation();
            compute1.init();
            compute1.readEffort();
            compute1.graphRecomendation(userID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * Display player utility as a line graph
     */
    public static void graphUtility(int userID) {

        XYSeries series = new XYSeries("Utility");
        double[][] data = null;
        try {

            data = DataAccess.readDataFromDataTable(userID);

            // Date date = new Date();
            //double base_days = ((date.getYear() - 71) * 365 + 9 + 1 + date.getMonth() * 30 + date.getDay()); //Days
            //double base_ms = base_days * 24 * 60 * 60 * 1000;

            for (int i = 0; i < data.length; i++) {

                series.add(i, data[i][0]);
            }
            String graphTitle = "Utility Time plot for Player " + userID;
            XYDataset xyDataset = new XYSeriesCollection(series);
            JFreeChart chart = ChartFactory.createXYLineChart(
                    graphTitle, "Time", "Utility",
                    xyDataset, PlotOrientation.VERTICAL, true, true, false);
            chart.setBackgroundPaint(Color.WHITE);

            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            String filePath = "H:/code/Outsourced_Partial_Recommend/web/util_plot_user" + Integer.toString(userID) + ".png";
            File imgFile = new File(filePath);
            ChartUtilities.saveChartAsPNG(imgFile, chart, 600, 400, info);
            //  Game.displayMultiple();
        } catch (Exception e) {
            System.out.println(e);
        }

    }


    /*
     * Display player and subtree utility as a line graph
     */
    public static void displayMultipleUtils(int userID) {
        double[][] data;
        String graphTitle = "Utility Time plot for Player " + userID;
        double[][] temp1, temp2;
        try {

            temp1 = DataAccess.readDataFromDataTable(userID);
            temp2 = DataAccess.readSubTreeUtil(userID);
            data = new double[2][temp1.length];
            for (int i = 0; i < temp1.length; i++) {

                data[0][i] = temp1[i][0];
                data[1][i] = temp2[i][0];
            }
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Utility",
                    "", data);
            JFreeChart chart = ChartFactory.createLineChart(
                    graphTitle, "Time", "Utility",
                    dataset, PlotOrientation.VERTICAL, true, true, false);

            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            String filePath = "H:/code/Outsourced_Partial_Recommend/web/util_plot_user" + Integer.toString(userID) + ".png";
            File imgFile = new File(filePath);
            ChartUtilities.saveChartAsPNG(imgFile, chart, 700, 400, info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * Displays player's utility as a barchart( single bar)
     */
    public static void displayBarChart(int userID, int hint) {

        double[][] data = new double[1][1];
        String chartTitle = "Utility Bar for Player " + userID;
        double effort = 0;
        double[][] temp;
        try {

            temp = DataAccess.readDataFromDataTable(userID);
            effort = DataAccess.getLatestEffortforUser(userID);
            data[0][0] = temp[temp.length - 1][0];
            chartTitle += " Effort: " + effort + "\n";
            if(hint == 1){
                chartTitle+="Hint: Increase effort";
            }if(hint == -1){
                chartTitle+="Hint: Decrease effort";
            }if(hint == 0){
                chartTitle +="Hint: Don't change";
            }
            
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset("",
                    "", data);
            JFreeChart chart = ChartFactory.createBarChart(
                    chartTitle, "Player " + userID, "Utility",
                    dataset, PlotOrientation.VERTICAL, true, true, false);

            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            String filePath = "H:/code/Outsourced_Partial_Recommend/web/util_plot_user" + Integer.toString(userID) + ".png";
            File imgFile = new File(filePath);
            ChartUtilities.saveChartAsPNG(imgFile, chart, 150, 600, info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
